{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>大纲<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#定义descriptor\" data-toc-modified-id=\"定义descriptor-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>定义descriptor</a></span></li><li><span><a href=\"#使用descriptor\" data-toc-modified-id=\"使用descriptor-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>使用descriptor</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 定义descriptor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-29T10:04:36.647185Z",
     "start_time": "2018-05-29T10:04:36.643496Z"
    }
   },
   "outputs": [],
   "source": [
    "class WebFramework(object):\n",
    "    def __init__(self, name='Flask'):\n",
    "        self.name = name\n",
    "\n",
    "    def __get__(self, instance, owner):\n",
    "        return self.name\n",
    "\n",
    "    def __set__(self, instance, value):\n",
    "        self.name = value\n",
    "\n",
    "\n",
    "class Site(object):\n",
    "    webframework = WebFramework()\n",
    "    version = 0.1\n",
    "    def __init__(self,value):\n",
    "        self.site = value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用descriptor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-29T10:04:37.093824Z",
     "start_time": "2018-05-29T10:04:37.089019Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Flask'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "webframwork = WebFramework()\n",
    "webframwork.__get__(webframwork,WebFramework)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-29T10:05:33.295532Z",
     "start_time": "2018-05-29T10:05:33.292842Z"
    }
   },
   "outputs": [],
   "source": [
    "site1 = Site('pypi')\n",
    "site2 = Site('github')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-29T10:09:56.428991Z",
     "start_time": "2018-05-29T10:09:56.426281Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.1\n",
      "1.1\n",
      "1.1\n"
     ]
    }
   ],
   "source": [
    "print(Site.version)\n",
    "print(site1.version)\n",
    "print(site2.version)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-29T10:10:25.116643Z",
     "start_time": "2018-05-29T10:10:25.113623Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "site\n",
      "site\n",
      "1.1\n"
     ]
    }
   ],
   "source": [
    "Site.version = 1.1\n",
    "print(site1.version)\n",
    "site1.version = 'site'\n",
    "print(site1.version)\n",
    "print(site2.version)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "大纲",
   "title_sidebar": "目录",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
